package com.xypower.graph;

public class GraphGenerator {
    //martrix 所有的边
    //N*3的矩阵
    public static  Graph createGraph(Integer[][] matrix){
        Graph graph = new Graph();
        for (int i = 0;i < matrix.length;i++){//matrix[0][0],matrix[0][1],matrix[0][2]
            Integer from = matrix[i][0];
            Integer to = matrix[i][1];
            Integer weight = matrix[i][2];
            //如果from没有，则当前from点为第一次出现，把它放到点集中
            if (!graph.nodes.containsKey(from)){
                graph.nodes.put(from,new Node(from));
            }
            //如果to没有，则当前to点为第一次出现，把它放到点集中
            if (!graph.nodes.containsKey(to)){
                graph.nodes.put(to,new Node(to));
            }
            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            Edge newEdge = new Edge(weight,fromNode,toNode);
            fromNode.nexts.add(toNode);//fromNode的邻接点加上toNode
            fromNode.out++;             //fromNode出度+1
            toNode.in++;                //toNode的入度+1
            fromNode.edges.add(newEdge);//fromNode的边集加边
            graph.edges.add(newEdge);   //图的边集加边
        }
        return graph;
    }
}
